package com.bys.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bys.exception.BysOaSystemException;
import com.bys.response.Result;
import com.bys.system.model.SysRoleModel;
import com.bys.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Lang;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author bys
 * @since 2023-03-31
 */

@Api(tags = "系统角色接口")
@RestController
@RequestMapping("/system/sysRole")
public class SysRoleController {

    @Resource
    private ISysRoleService sysRoleService;

    @ApiOperation(value = "通过id 获取 角色", notes = "参数 id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        return Result.success(sysRoleService.getById(id));
    }

    @ApiOperation(value = "获取所有的 角色", notes = "没有参数")
    @GetMapping("/findAll")
    public Result findAllRole() {
        return Result.success(sysRoleService.list());
    }

    @ApiOperation(value = "分页查询角色对象", notes = "pageNo 当前页， pageSize 每页大小,sysRoleModel 查询对象 ")
    @GetMapping("/{pageNo}/{pageSize}")
    public Result getPageList(@PathVariable Integer pageNo, @PathVariable Integer pageSize, SysRoleModel sysRoleModel) {
        Page<SysRoleModel> data = sysRoleService.getRolePage(pageNo, pageSize, sysRoleModel);
        return Result.success(data);
    }

    @ApiOperation(value = "删除 角色", notes = "参数 id")
    @DeleteMapping("/remove/{id}")
    public Result removeRole(@PathVariable Long id) {
        Long updateUser = 0L;
        sysRoleService.deleteRoleLogic(id, updateUser);
        return Result.success();
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody SysRoleModel sysRoleModel) {
        sysRoleModel.setCreateTime(LocalDateTime.now());
        sysRoleService.save(sysRoleModel);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "更新角色")
    @PutMapping("/update")
    public Result update(@RequestBody SysRoleModel sysRoleModel) {
        sysRoleService.updateById(sysRoleModel);
        return Result.success();
    }

    @ApiOperation(value = "批量删除 id", notes = "参数  集合 ids")
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody  List<Long> ids) {
        Optional.ofNullable(ids).orElseThrow(() -> new BysOaSystemException("没有选择id"));
        for (Long id : ids) {
            sysRoleService.deleteRoleLogic(id, 0L);
        }
        return Result.success();
    }
}
